# 导入:
from sqlalchemy import Column, String, BIGINT, UniqueConstraint,BOOLEAN,SmallInteger,text,ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from helper.ConnectionHelper import ConnectionHelper
connectionHelper=ConnectionHelper()
session,engine=connectionHelper.getSessionAndEngine()

# 创建对象的基类:
Base = declarative_base()


#用户表
class User(Base):
    __tablename__='tb_user'
    id = Column(BIGINT, primary_key=True)  # 自增id
    job_number=Column(String(100),nullable=False,unique=True) #工号
    user_name=Column(String(100),nullable=False)
    password=Column(String(100),nullable=False)
    access_level=Column(SmallInteger,server_default=text('3')) #权限 1，表示管理员，2表示系主任，3表示老师
    def toDict(self):
        return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}


#著作教材
class Book(Base):
    __tablename__='tb_book'
    id = Column(BIGINT, primary_key=True)  # 自增id
    job_number=Column(String(100),nullable=False)
    result_name=Column(String(100),nullable=False)
    result_form=Column(SmallInteger,nullable=False) #1、	教材 2、	专著 3、	译著 4、	工具书
    press_name=Column(String(100))
    press_time=Column(String(100))
    press_count=Column(String(100))
    isbn_number=Column(String(100))
    press_level=Column(String(100))
    author_is_eic=Column(String(100))
    own_words=Column(String(100))
    total_words=Column(String(100))
    UniqueConstraint(job_number,result_name)

    def toDict(self):
        return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}

    # Base.toDict = toDict






# 培训进修、交流
class Exchange(Base):
    __tablename__ = 'tb_exchange'

    id = Column(BIGINT, primary_key=True)  # 自增id
    job_number=Column(String(100),nullable=False)
    teacher_name=Column(String(100))
    train_type=Column(SmallInteger,nullable=False) #1、企业挂职 2、学术交流 3、国内访学 4、国外学术交流 5、国外访学 6、国内参会
    start_time=Column(String(100),nullable=False)
    end_time=Column(String(100))
    content=Column(String(100))
    theme=Column(String(100))
    UniqueConstraint(job_number,train_type,start_time)
    def toDict(self):
        return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}


# 自然科研学奖
#更名：教学科学奖
class TeachingScience(Base):
    __tablename__='tb_teaching_science'
    id = Column(BIGINT, primary_key=True)  # 自增id
    job_number=Column(String(100),nullable=False)
    result_name=Column(String(100),nullable=False)
    result_form=Column(SmallInteger,nullable=False)
    units_issued=Column(String(100))
    award_type=Column(String(100))
    award_grade=Column(String(100))
    award_year=Column(String(100))
    order=Column(String(100))
    total_people=Column(String(100))
    UniqueConstraint(job_number,result_name)
    def toDict(self):
        return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}




# 论文
class Paper(Base):
    __tablename__ = 'tb_paper'
    id = Column(BIGINT, primary_key=True)  # 自增idtotal_people
    job_number=Column(String(100),nullable=False)
    result_name=Column(String(100))
    result_form=Column(String(100))
    publication_name=Column(String(100))
    issuing_time=Column(String(100))
    isbn_number=Column(String(100),nullable=False)
    publication_level=Column(SmallInteger,nullable=False)
    search_info=Column(String(100))
    order=Column(String(100))
    total_people=Column(String(100))
    UniqueConstraint(job_number,isbn_number)
    def toDict(self):
        return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}



#专利
class Patent(Base):
    __tablename__='tb_patent'
    id = Column(BIGINT, primary_key=True)  # 自增id
    job_number=Column(String(100),nullable=False)
    result_name=Column(String(100),nullable=False)
    result_form=Column(SmallInteger,nullable=False)
    units_issued=Column(String(100))
    patent_type=Column(String(100))
    patent_number=Column(String(100))
    patent_year=Column(String(100))
    award_time=Column(String(100))
    order=Column(String(100))
    total_people=Column(String(100))
    UniqueConstraint(job_number,result_name)
    def toDict(self):
        return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}







#个人或团队荣誉
class PersonalTeam(Base):
    __tablename__ = 'tb_personal_team'
    id = Column(BIGINT, primary_key=True)  # 自增id
    job_number=Column(String(100),nullable=False)
    title=Column(String(100),nullable=False)
    honor_type=Column(SmallInteger) #1表示国家级,2表示市级,3表示校级
    name=Column(String(100))
    time=Column(String(100))
    rewards_bureau=Column(String(100))
    theme=Column(String(100))
    UniqueConstraint(job_number,title)
    def toDict(self):
        return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}





#项目
class Project(Base):
    __tablename__='tb_project'
    id = Column(BIGINT, primary_key=True)  # 自增id
    job_number=Column(String(100),nullable=False)
    presenter_flag=Column(SmallInteger)  #0不是，1是
    member=Column(String(100))
    project_name=Column(String(100))
    fee=Column(String(100))
    project_number=Column(String(100),nullable=False)
    remark=Column(String(100))
    project_start_time=Column(String(100))
    project_end_time=Column(String(100))
    project_type=Column(SmallInteger) #1、	横向项目 2、	项目项目 3、	教改项目
    order=Column(SmallInteger)
    support_unit=Column(String(100))
    UniqueConstraint(job_number,project_number)
    def toDict(self):
        return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}


# 学科竞赛
class Subject(Base):
    __tablename__ = 'tb_subject'
    id = Column(BIGINT, primary_key=True)  # 自增id
    job_number=Column(String(100),nullable=False)
    competition_name=Column(String(100),nullable=False)
    teams=Column(String(100))
    award_level=Column(String(100))
    advisor=Column(String(100))
    remark=Column(String(100))
    certificate_copy=Column(String(100))
    number=Column(String(100))
    UniqueConstraint(job_number,competition_name)
    def toDict(self):
        return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}





#文件
class File(Base):
    __tablename__="tb_file"
    id = Column(BIGINT, primary_key=True)  # 自增id
    job_number = Column(String(100))
    year = Column(String(100))
    excel_type = Column(String(100))
    type_file_id=Column(String(100))
    file_name=Column(String(100))
    UniqueConstraint(type_file_id)
    def toDict(self):
        return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}


#创建表格
Base.metadata.create_all(engine)

